﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using NLBS.Entity;

namespace NLBS.Data
{
    public partial class SqlDataProvider : DbProvider
    {
        #region WordFilter

        public override IList<WordFilter> GetWordFilter()
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("SELECT wf_id, wf_mode, wf_text, wf_replace, wf_regExp FROM dbo.blog_WordFilter");

            IList<WordFilter> lists = new List<WordFilter>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    WordFilter wf = CommonDataProvider.PopulateWrodFilterFromIDataReader(dataReader);
                    lists.Add(wf);
                }
            }
            return lists;
        }

        public override WordFilter GetWordFilterByID(int itemID)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"SELECT   wf_id, wf_mode, wf_text, wf_replace, wf_regExp
            FROM      blog_WordFilter
            WHERE   (wf_id = @itemID)");
            db.AddInParameter(dbCommand, "itemID", DbType.Int32, itemID);

            WordFilter wf = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    wf = CommonDataProvider.PopulateWrodFilterFromIDataReader(dataReader);
                }
            }
            return wf;
        }

        public override int InsertWordFilter(WordFilter item)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"INSERT INTO [dbo].[blog_WordFilter] ([wf_mode], [wf_text], [wf_replace], [wf_regExp]) VALUES (@mode, @text, @replace, @regExp)
            SELECT @wfID=@@IDENTITY");
            db.AddInParameter(dbCommand, "mode", DbType.Byte, item.Mode);
            db.AddInParameter(dbCommand, "text", DbType.String, item.Text);
            db.AddInParameter(dbCommand, "replace", DbType.String, item.Replace);
            db.AddInParameter(dbCommand, "regExp", DbType.Boolean, item.RegExp);
            db.AddOutParameter(dbCommand, "wfID", DbType.Boolean, 10);
            db.ExecuteNonQuery(dbCommand);
            return Convert.ToInt32(db.GetParameterValue(dbCommand, "@wfID"));
        }

        public override void UpdateWordFilter(WordFilter item)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"UPDATE [dbo].[blog_WordFilter] SET [wf_mode] = @mode, [wf_text] = @text, 
            [wf_replace] = @replace, [wf_regExp] = @regExp WHERE (([wf_id] = @id))");
            db.AddInParameter(dbCommand, "id", DbType.String, item.WordFilterID);
            db.AddInParameter(dbCommand, "mode", DbType.Byte, item.Mode);
            db.AddInParameter(dbCommand, "text", DbType.String, item.Text);
            db.AddInParameter(dbCommand, "replace", DbType.String, item.Replace);
            db.AddInParameter(dbCommand, "regExp", DbType.Boolean, item.RegExp);
            db.ExecuteNonQuery(dbCommand);
        }

        public override void DeleteWordFilter(int itemID)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("DELETE FROM [blog_WordFilter] WHERE (([wf_id] = @id))");
            db.AddInParameter(dbCommand, "id", DbType.String, itemID);
            db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}
